﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ucQuestion : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDropDown();
            BindGrid(DropDownList2.SelectedValue);
            BindSubGrid();
        }
    }

    private void BindDropDown()
    {
        CategoryBL obj = new CategoryBL();
        DropDownList1.DataSource = obj.GetMainCategories();
        DropDownList1.DataTextField = "CATEGORY_NAME";
        DropDownList1.DataValueField = "CATEGORY_ID";
        DropDownList1.DataBind();
        DropDownList1.Items.Insert(0, new ListItem("-Select-", "0"));
        DropDownList1.SelectedValue = "0";
        DropDownList2.Items.Insert(0, new ListItem("-Select-", "0"));
        DropDownList2.SelectedValue = "0";
    }

    private void BindGrid(string parentCategory)
    {
        QuestionBL obj = new QuestionBL();
        GridView1.DataSource = obj.GetQuestions(parentCategory);
        GridView1.DataBind();
    }

    private void BindSubGrid()
    {
        QuestionBL obj = new QuestionBL();
        foreach (GridViewRow item in GridView1.Rows)
        {
            GridView gvr2 = (GridView)item.FindControl("GridView2");
            long questionId = Convert.ToInt64(GridView1.DataKeys[item.RowIndex].Values[0]);
            gvr2.DataSource = obj.GetAnswer(questionId);
            gvr2.DataBind();
        }
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        CategoryBL obj = new CategoryBL();
        DropDownList2.DataSource = obj.GetSubCategories(DropDownList1.SelectedValue);
        DropDownList2.DataTextField = "CATEGORY_NAME";
        DropDownList2.DataValueField = "CATEGORY_ID";
        DropDownList2.DataBind();
        DropDownList2.Items.Insert(0, new ListItem("-Select-", "0"));
        DropDownList2.SelectedValue = "0";
        //BindGrid(DropDownList2.SelectedValue);
    }
    protected void btnAddQuestion_Click(object sender, EventArgs e)
    {
        if (DropDownList2.SelectedValue != "0")
        {
            var login = (LoginBL)Session["User"];
            string path = string.Empty;
            QuestionBL obj = new QuestionBL(txtQuestionText.Text, path, DropDownList2.SelectedValue, login.UserId);
            if (obj.Insert())
            {
                BindGrid(DropDownList2.SelectedValue);
                BindSubGrid();
            }
            else
            {
                Response.Write("<script>alert('Server Problem! Try Again.')</script>");
            }
        }
        else
        {
            Response.Write("<script>alert('Choose Category Name.')</script>");
        }
    }
    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindGrid(DropDownList2.SelectedValue);
        BindSubGrid();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        BindGrid(DropDownList2.SelectedValue);
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        long questionId = Convert.ToInt64(GridView1.DataKeys[e.RowIndex].Values[0]);
        TextBox TextBox1 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1");
        var login = (LoginBL)Session["User"];
        QuestionBL obj = new QuestionBL(questionId, TextBox1.Text, DropDownList2.SelectedValue, login.UserId);
        if (obj.Update())
        {
            BindGrid(DropDownList2.SelectedValue);
        }
        else
        {
            Response.Write("<script>alert('Server Problem! Try Again.')</script>");
        }
        GridView1.EditIndex = -1;
        BindGrid(DropDownList2.SelectedValue);
        BindSubGrid();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        BindGrid(DropDownList2.SelectedValue);
        BindSubGrid();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        long questionId = Convert.ToInt64(GridView1.DataKeys[e.RowIndex].Values[0]);
        QuestionBL obj = new QuestionBL(questionId);
        if (obj.Delete())
        {
            BindGrid(DropDownList2.SelectedValue);
            BindSubGrid();
        }
        else
        {
            Response.Write("<script>alert('Server Problem! Try Again.')</script>");
        }
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "AddAnswer")
        {
            long questionId = Convert.ToInt64(e.CommandArgument);
            GridViewRow rr = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
            HiddenField HiddenField1 = (HiddenField)rr.FindControl("HiddenField1");
            HiddenField1.Value = questionId.ToString();
            Panel Panel1 = (Panel)rr.FindControl("Panel1");
            Panel1.Visible = true;
        }
        if (e.CommandName == "AnswerSave")
        {
            GridViewRow rr = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
            TextBox TextBox2 = (TextBox)rr.FindControl("TextBox2");
            HiddenField HiddenField1 = (HiddenField)rr.FindControl("HiddenField1");
            CheckBox CheckBox1 = (CheckBox)rr.FindControl("CheckBox1");
            var login = (LoginBL)Session["User"];
            AnswerBL obj = new AnswerBL(TextBox2.Text, HiddenField1.Value, CheckBox1.Checked, login.UserId);
            if (obj.Insert())
            {
                HiddenField1.Value = "";
                Panel Panel1 = (Panel)rr.FindControl("Panel1");
                TextBox2.Text = string.Empty;
                Panel1.Visible = false;
                BindGrid(DropDownList2.SelectedValue);
                BindSubGrid();
            }
            else
            {
                Response.Write("<script>alert('Server Problem! Try Again.')</script>");
            }
        }
        if (e.CommandName == "AnswerCancel")
        {
            GridViewRow rr = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
            HiddenField HiddenField1 = (HiddenField)rr.FindControl("HiddenField1");
            TextBox TextBox2 = (TextBox)rr.FindControl("TextBox2");
            HiddenField1.Value = "";
            Panel Panel1 = (Panel)rr.FindControl("Panel1");
            TextBox2.Text = string.Empty;
            Panel1.Visible = false;
        }
    }
    protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView GridView2 = (GridView)sender;
        GridView2.EditIndex = e.NewEditIndex;
        BindSubGrid();
    }
    protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridView GridView2 = (GridView)sender;
        long answerId = Convert.ToInt64(GridView2.DataKeys[e.RowIndex].Values[0]);
        TextBox TextBox2 = (TextBox)GridView2.Rows[e.RowIndex].FindControl("TextBox2");
        RadioButton RadioButton1 = (RadioButton)GridView2.Rows[e.RowIndex].FindControl("RadioButton1");
        var login = (LoginBL)Session["User"];
        GridViewRow gvr = (GridViewRow)GridView2.Parent.Parent;
        AnswerBL obj = new AnswerBL(answerId, TextBox2.Text, GridView1.DataKeys[gvr.RowIndex].Values[0].ToString(), RadioButton1.Checked, login.UserId);
        if (obj.Update())
        {
            BindSubGrid();
        }
        else
        {
            Response.Write("<script>alert('Server Problem! Try Again.')</script>");
        }
        GridView2.EditIndex = -1;
        BindSubGrid();
    }
    protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        GridView GridView2 = (GridView)sender;
        long answerId = Convert.ToInt64(GridView2.DataKeys[e.RowIndex].Values[0]);
        AnswerBL obj = new AnswerBL(answerId);
        if (obj.Delete())
        {
            BindSubGrid();
        }
        else
        {
            Response.Write("<script>alert('Server Problem! Try Again.')</script>");
        }
    }
    protected void GridView2_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView GridView2 = (GridView)sender;
        GridView2.EditIndex = -1;
        BindSubGrid();
    }
}